/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.sys.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.Constant;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.Assert;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.sys.entity.SysOrgEntity;
import io.renren.modules.sys.form.PasswordForm;
import io.renren.modules.sys.service.SysEmployeeService;
import io.renren.modules.sys.service.SysOrgService;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 系统组织
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/sys/org")
public class SysOrgController extends AbstractController {
	@Autowired
	private SysOrgService sysOrgService;
	@Autowired
	private SysEmployeeService sysEmployeeService;


	/**
	 * 所有组织列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("sys:org:list")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils page = sysOrgService.queryPage(params);
		List list = page.getList();
		for(int i=0;i<list.size();i++){
			SysOrgEntity org = (SysOrgEntity)list.get(i);
			if(org.getOrgParentId()!=null){
				org.setOrgParentName(sysOrgService.getById(org.getOrgParentId()).getOrgName());
			}else{
				org.setOrgParentName("/");
			}
		}
		return R.ok().put("page", page);
	}

	/**
	 * 组织信息
	 */
	@GetMapping("/info/{orgId}")
	@RequiresPermissions("sys:org:info")
	public R info(@PathVariable("orgId") Long orgId){
		SysOrgEntity org = sysOrgService.getById(orgId);
		if(org.getOrgParentId()!=null){
			org.setOrgParentName(sysOrgService.getById(org.getOrgParentId()).getOrgName());
		}
		return R.ok().put("org", org);
	}
	
	/**
	 * 保存组织
	 */
	@SysLog("保存组织")
	@PostMapping("/save")
	@RequiresPermissions("sys:org:save")
	public R save(@RequestBody SysOrgEntity org){
		ValidatorUtils.validateEntity(org, AddGroup.class);
		sysOrgService.saveOrg(org);
		
		return R.ok();
	}
	
	/**
	 * 修改组织
	 */
	@SysLog("修改组织")
	@PostMapping("/update")
	@RequiresPermissions("sys:org:update")
	public R update(@RequestBody SysOrgEntity org){
		ValidatorUtils.validateEntity(org, UpdateGroup.class);
		sysOrgService.update(org);
		
		return R.ok();
	}
	
	/**
	 * 删除组织
	 */
	@SysLog("删除组织")
	@PostMapping("/delete")
	@RequiresPermissions("sys:org:delete")
	public R delete(@RequestBody Long[] orgIds){
		if(ArrayUtils.contains(orgIds, 1L)){
			return R.error("根组织不能删除");
		}
		
		if(ArrayUtils.contains(orgIds, sysEmployeeService.getById(getUser().getEmpId()).getOrgId())){
			return R.error("当前组织不能删除");
		}
		sysOrgService.deleteBatch(orgIds);
		
		return R.ok();
	}
}
